package com.gem.shop.web;

import com.gem.shop.entity.Commodity;
import com.gem.shop.entity.Customer;
import com.gem.shop.entity.Order;
import com.gem.shop.entity.OrderDetails;
import com.gem.shop.service.CommodityService;
import com.gem.shop.service.OrderService;
import com.gem.shop.util.BeanFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author zhang
 * @date 2020/10/3 14:46
 */
@WebServlet("/permission/order/*")
public class OrderServlet extends HttpServlet {

    OrderService orderService = (OrderService) BeanFactory.getBean("orderService");
    CommodityService commodityService = (CommodityService) BeanFactory.getBean("commodityService");

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();

        if ("/buy".equals(pathInfo)){

            HttpSession session = req.getSession();
            Customer customer = (Customer) session.getAttribute("customer");

            String idStr = req.getParameter("id");
            String numStr = req.getParameter("num");
            int id = Integer.parseInt(idStr.trim());
            int num = Integer.parseInt(numStr.trim());

            Commodity commodity = commodityService.findCommodityById(id);

            OrderDetails orderDetails = new OrderDetails();
            orderDetails.setNum(num);
            orderDetails.setPrice(commodity.getPrice());
            orderDetails.setCommodity(commodity);
            orderDetails.setSubtotal(commodity.getPrice() * num);
            List<OrderDetails> list = new ArrayList<>();
            list.add(orderDetails);

            Order order = new Order();
            order.setOrderId(UUID.randomUUID().toString().replace("-",""));
            order.setOrderDetails(list);
            order.setCustomer(customer);
            order.setCreateTime(LocalDateTime.now());
            orderDetails.setOrder(order);


            orderService.addOrder(order);

            PrintWriter out = resp.getWriter();
            out.print("1");
        }


        if ("/toSuccess".equals(pathInfo)){
            req.getRequestDispatcher("/WEB-INF/pages/successBuy.jsp").forward(req,resp);
        }

        if ("/showMyOrders".equals(pathInfo)){
            HttpSession session = req.getSession();
            Customer customer = (Customer) session.getAttribute("customer");
            List<Order> orders = orderService.findOrderByCustomerId(customer.getId());
            List<OrderDetails> orderDetailsList = new ArrayList<>();
            for (Order order : orders) {
                List<OrderDetails> orderDetails = order.getOrderDetails();
                for (OrderDetails o : orderDetails) {
                    orderDetailsList.add(o);
                }
            }
            req.setAttribute("orderDetailsList",orderDetailsList);
            req.getRequestDispatcher("/WEB-INF/pages/myOrder.jsp").forward(req,resp);
        }

        //结算购物车
        if ("/settlement".equals(pathInfo)){
            HttpSession session = req.getSession();
            Customer customer = (Customer) session.getAttribute("customer");
            List<OrderDetails> orderDetailsList = (List<OrderDetails>) session.getAttribute("cart");

            Order order = new Order();
            order.setCreateTime(LocalDateTime.now());
            order.setCustomer(customer);
            order.setOrderDetails(orderDetailsList);
            order.setOrderId(UUID.randomUUID().toString().replace("-",""));


            for (OrderDetails orderDetails : orderDetailsList) {
                orderDetails.setOrder(order);
            }

            orderService.addOrder(order);


            //情况session
            session.removeAttribute("cart");

            PrintWriter out = resp.getWriter();
            out.print("1");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
